/*
 * @lc app=leetcode.cn id=844 lang=swift
 *
 * [844] 比较含退格的字符串
 */

// @lc code=start
class Solution {
    func backspaceCompare(_ s: String, _ t: String) -> Bool {
        var ss = Array(s)
        var left = 0, right = 0
        while right < ss.count {
            if left < 0 {
                left = 0
            }
            if ss[right] == "#" {
                right += 1
                left -= 1
                continue
            }
            if left != right {
                ss[left] = ss[right]
            }
            left += 1
            right += 1
        }
        let newS1 = left < 0 ? "" : String(ss[0..<left])

        var ts = Array(t)
        left = 0
        right = 0
        while right < ts.count {
            if left < 0 {
                left = 0
            }
            if ts[right] == "#" {
                right += 1
                left -= 1
                continue
            }
            if left != right {
                ts[left] = ts[right]
            }
            left += 1
            right += 1
        }

        let newS2 = left < 0 ? "" : String(ts[0..<left])

        print("\(newS1) - \(newS2)")
        return newS1 == newS2
    }
}
// @lc code=end

